package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbAddressMapper;
import com.dongdongshop.page.PageBean;
import com.dongdongshop.pojo.TbAddress;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.AddressService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@DubboService
public class AddressServiceImpl implements AddressService {

    @Resource
    private TbAddressMapper tm;

    @Override
    public PageBean getAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<TbAddress> list = tm.selectByExample(null);
        PageInfo info = new PageInfo(list);
        return new PageBean(pageNum,pageSize,info.getTotal(),info.getPages(),info.getList());
    }

    @Override
    public int insertAddress(TbAddress address, TbUser user) {
        address.setUserId(user.getUsername());
        address.setCreateDate(new Date());
        return tm.insertSelective(address);
    }

    @Override
    public TbAddress getAddressById(Long id) {
        return tm.selectByPrimaryKey(id);
    }

    @Override
    public int updateAddress(TbAddress address) {
        return tm.updateByPrimaryKey(address);
    }

    @Override
    public int delete(Long id) {
        return tm.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteCheck(Long[] ids) {
        for (Long id : ids) {
            delete(id);
        }
        return 1;
    }
}
